var express = require('express');
var router = express.Router();
const breadService = require('../service/breadService');

/* 获取面包数据 */
router.get('/getbread', async function (req, res) {
  let { current, pageSize ,type,value} = req.query;
  let searchParams = {};
  if (type) {
     if (type == "age") {
        searchParams[type] = value;
     } else {
        searchParams[type] = { $regex: value };
     }
  }
  console.log(req.query);
  let data = await breadService.find({ current: current || 1, pageSize: pageSize || 5 },searchParams);
  res.send(data);
});


// id查找
router.get('/findbyid',async function(req,res){
  let {id} = req.query;
  let data = await breadService.findById(id);
  res.send(data);
});


// 添加
router.post('/insert', async function (req, res) {
  let { name, price, intro, tips, mixed, reminder, total, images, storeId,spare } = req.body;
  console.log(name, price, intro, tips, mixed, reminder, total, images, storeId,spare );
  let data = await breadService.insert({name, price, intro, tips, mixed, reminder, total, images, storeId,spare  });
  res.send(data);
});


//删除
router.delete('/remove', async function (req, res) {
  let { id } = req.body;
  console.log(id);
  let data = await breadService.delete(id);
  res.send(data);
});

// 修改
router.put('/update',async function(req,res){
  let {_id,name, price, intro, tips, mixed, reminder, total, images, storeId,spare } = req.body;
  console.log(_id,name, price, intro, tips, mixed, reminder, total, images, storeId,spare)
  let data = await breadService.update({_id,name, price, intro, tips, mixed, reminder, total, images, storeId,spare});
  res.send(data);
});


module.exports = router;
